#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QMessageBox>
#include <QFileDialog>
#include <cmath>
#include <QtMath>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    
    // 创建绘图部件
//    m_plotWidget = new PolarPlotWidget();
//    m_plotWidget->resize(800,800);
    m_plotWidget = new PolarPlotWidget(this);
    setCentralWidget(m_plotWidget);
    
    // 生成示例数据
    generateSampleData();

    m_plotWidget->setData(m_polarData);
    
    // 创建菜单栏保存动作
    QAction *saveAction = new QAction("保存图片", this);
    connect(saveAction, &QAction::triggered, this, &MainWindow::onSaveImage);
    
    QMenu *fileMenu = menuBar()->addMenu("文件");
    fileMenu->addAction(saveAction);
    
    setWindowTitle("极坐标绘图器");
    resize(800, 800);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::generateSampleData()
{
    // 生成示例极坐标数据 - 这里创建一个简单的心形曲线
    m_polarData.clear();
    
    const int pointsCount = 360;  // 360个点
    for (int i = 0; i < pointsCount; ++i) {
        double angle = 2 * M_PI * i / pointsCount;  // 角度(弧度)
        // 心形曲线的极坐标方程: r = a(1 - sinθ)
        double radius = 100 * (1 - sin(angle));
        
        m_polarData.append({angle, radius});
    }
}

void MainWindow::onSaveImage()
{
#if 0
    QString filePath = QFileDialog::getSaveFileName(
        this, "保存图片", "", "PNG图片 (*.png);;JPEG图片 (*.jpg);;所有文件 (*)"
    );
    
    if (!filePath.isEmpty()) {
        if (m_plotWidget->saveImage(filePath)) {
            QMessageBox::information(this, "成功", "图片保存成功!");
        } else {
            QMessageBox::warning(this, "失败", "图片保存失败!");
        }
    }
#endif
    QString filePath = QString(".\\pngSonarScan.png");;
    if (m_plotWidget->saveImage(filePath)) {
        QMessageBox::information(this, "成功", "图片保存成功!");
    } else {
        QMessageBox::warning(this, "失败", "图片保存失败!");
    }
}

